package com.siwei.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.ProjectArea;
import com.siwei.mes.entity.User;
import com.siwei.mes.mapper.ProjectAreaMapper;
import com.siwei.mes.mapper.UserMapper;
import com.siwei.mes.service.AuthService;
import com.siwei.mes.service.UserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 用户表(User)表服务实现类
 *
 * @author linzi
 * @since 2023-11-25 18:16:11
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private AuthService authService;
    @Resource
    private ProjectAreaMapper projectAreaMapper;

    @Override
    public Long create(User entity) {
        /**判断手机号是否存在*/
        assertTrue(userMapper.selectByUserPhone(entity.getUserPhone()) != null, "手机号已经存在!");
        entity.init(true);
        if (StringUtils.isBlank(entity.getPassword())) {
            entity.setPassword(BaseParams.PASSWORD);
        }
        userMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(User entity) {
        entity.init(false);
        User user = userMapper.selectByPrimaryKey(entity.getId());
        assertTrue(user == null, "用户表信息不存在");
        if (!entity.getUserPhone().equals(user.getUserPhone())) {
            /**判断手机号是否存在*/
            assertTrue(userMapper.selectByUserPhone(user.getUserPhone()) != null, "手机号已经存在");
        }
        userMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void updatePassword(User entity) {
        entity.init(false);
        User user = userMapper.selectByPrimaryKey(entity.getId());
        assertTrue(user == null, "用户表信息不存在");
        assertTrue(!user.getPassword().equals(entity.getOldPassword()), "原密码错误");
        user.setPassword(entity.getPassword());
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void delete(Long id) {
        User user = userMapper.selectByPrimaryKey(id);
        assertTrue(user == null, "用户表信息不存在");
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<User> getPage(PageQuery<User, User> query) {
        User searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> userMapper.getList(searchParam));
    }

    @Override
    public User loadById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> getAll(User entity) {
        if("1".equals(entity.getIsArea())){
            ProjectArea entityArea = new ProjectArea();
            entityArea.setHeadmanId(String.valueOf(authService.getLoginUserId()));
            List<ProjectArea> list = projectAreaMapper.getList(entityArea);
            if(CollectionUtils.isNotEmpty(list)){
                List<String> ids =  Arrays.asList(list.get(0).getCustomerServiceId().split(","));
                if(CollectionUtils.isEmpty(list)){
                    return null;
                }
                entity.setIds(ids);
            }else{
                return null;
            }
        }
        return userMapper.getList(entity);
    }
}
